﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

public partial class RequestDetail : System.Web.UI.Page
{
    private Boolean isNew = true;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            if (Request.Params.Get("rqid") == null)
                Response.Redirect("RequestForm.aspx");

            //update
            ProRequest pr = new ProRequest();
            DataSet dsRquestInfo = new DataSet();
            dsRquestInfo = pr.LoadRequestInfo(Request.Params.Get("rqid"));

            if (Session["user"].ToString() != dsRquestInfo.Tables[0].Rows[0]["username"].ToString())
            {
                this.lblError.Text = "You don't have permission to edit this request or you aren't submitter! Redirect to Review page after 5s";
                HtmlMeta metaTag = new HtmlMeta();
                metaTag.Attributes.Add("http-equiv", "refresh");
                metaTag.Attributes.Add("content", "5;url=RequestReview.aspx?rqid=" + Request.Params.Get("rqid"));
                HtmlHead head = (HtmlHead)Page.Header;
                head.Controls.Add(metaTag);
            }
            else
            {
                //hide calendar when default load
                this.cldDateRequest.Visible = false;
                this.cldDateRequest.SelectedDate = DateTime.Today;
                this.lblShowDate.Text = DateTime.Today.ToShortDateString();

                //load combobox
                LoadGridViewData();

                //set Button
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = true;
                this.btnNew.Enabled = false;
            }
        }
        else
        {
        }
    }
    private void SetHeaderText()
    {
        this.grdRequestDetail.HeaderRow.Cells[1].Text = "Request Code";
        this.grdRequestDetail.HeaderRow.Cells[1].Width = 200;
        this.grdRequestDetail.HeaderRow.Cells[2].Text = "ID";
        this.grdRequestDetail.HeaderRow.Cells[3].Text = "Date";
        this.grdRequestDetail.HeaderRow.Cells[4].Text = "Voucher";
        this.grdRequestDetail.HeaderRow.Cells[5].Text = "Description";
        this.grdRequestDetail.HeaderRow.Cells[6].Text = "Quantity";
        this.grdRequestDetail.HeaderRow.Cells[7].Text = "Amount(USD)";
        this.grdRequestDetail.HeaderRow.Cells[8].Text = "Ex.Rate";
        this.grdRequestDetail.HeaderRow.Cells[9].Text = "Amount(VND)";
        this.grdRequestDetail.HeaderRow.Cells[10].Text = "%VAT";
        this.grdRequestDetail.HeaderRow.Cells[11].Text = "Amount(VAT)";
        this.grdRequestDetail.HeaderRow.Cells[12].Text = "Total";
    }
    protected void imgCalendarButton_Click(object sender, ImageClickEventArgs e)
    {
        if (this.cldDateRequest.Visible == true)
            this.cldDateRequest.Visible = false;
        else
            this.cldDateRequest.Visible = true;
    }
    protected void cldDateRequest_SelectionChanged(object sender, EventArgs e)
    {
        this.lblShowDate.Text = this.cldDateRequest.SelectedDate.ToShortDateString();
    }

    protected void grdRequestDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillRequestDetail();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.btnSave.Enabled = true;
        this.btnDelete.Enabled = false;
        this.btnNew.Enabled = false;
        ClearAllFields();
        isNew = true;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ProRequest pr = new ProRequest();
        DataSet ds = pr.LoadRequestConfirmInfo(Request.Params.Get("rqid"));
        if (ds == null || ds.Tables[0].Rows.Count == 0 || (ds != null  && (ds.Tables[0].Rows[0]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[1]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[2]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString())))
        {
            this.btnSave.Enabled = false;
            this.btnDelete.Enabled = false;
            this.btnNew.Enabled = true;
            //Delete data with key.
            //Reload
            RequestDetailEntity rqd = FillRequestDetailEntity();
            pr.DeleteRequestDetail(rqd);
            LoadGridViewData();
        }
        else
        {
            this.lblError.Text = "This request is being processing! You can't modify.";
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ProRequest pr = new ProRequest();
        DataSet ds = pr.LoadRequestConfirmInfo(Request.Params.Get("rqid"));
        if (ds == null || ds.Tables[0].Rows.Count == 0 || (ds != null && (ds.Tables[0].Rows[0]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[1]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[2]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString())))
        {
            this.btnSave.Enabled = false;
            this.btnNew.Enabled = true;
            this.btnDelete.Enabled = false;

            RequestDetailEntity rqd = FillRequestDetailEntity();

            if (isNew)
            {
                //Insert to DB
                //Fill fields to Entity
                //Insert and reload.
                pr.SaveRequestDetail(rqd);
                LoadGridViewData();
            }
            else
            {
                //Update to DB
                //Fill fields to Entity
                //Update and reload
                pr.UpdateRequestDetail(rqd);
                LoadGridViewData();
            }
        }
        else
        {
            this.lblError.Text = "This request is being processing! You can't modify.";
        }
    }

    private void LoadGridViewData()
    {
        ProRequest pr = new ProRequest();
        //DataSet ds = pr.LoadRequestDetail("RF-20100204-001");
        DataSet ds = pr.LoadRequestDetail(Request.Params.Get("rqid"));
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdRequestDetail.DataSource = ds.Tables[0];
            this.grdRequestDetail.DataBind();
            this.grdRequestDetail.Rows[0].Visible = false;
        }
        else
        {
            //ds.Tables[0].Columns["date"].DataType = Type.;
            this.grdRequestDetail.DataSource = ds.Tables[0];
            this.grdRequestDetail.DataBind();
        }
        SetHeaderText();
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("RequestReview.aspx?rqid=" + Request.Params.Get("rqid"));
    }

    private RequestDetailEntity FillRequestDetailEntity()
    {
        RequestDetailEntity rqd = new RequestDetailEntity();
        rqd.StrDescription = this.txtDetail.Text;
        rqd.RqRequestCode = new RequestInfo();
        rqd.RqRequestCode.StrRequestCode = Request.Params.Get("rqid");
        rqd.DtDetailDate = this.cldDateRequest.SelectedDate;
        if (this.cbbVndUsd.SelectedItem.Text == Constant.STR_USD)
        {
            rqd.FAmountUSD = float.Parse(this.txtAmount.Text);
            rqd.FAmountVND = rqd.FAmountUSD * rqd.FExRate;
        }
        else
        {
            rqd.FAmountUSD = 0;
            rqd.FAmountVND = float.Parse(this.txtAmount.Text);
        }

        rqd.FExRate = float.Parse(this.txtExRate.Text);
        rqd.FQuantity = float.Parse(this.txtQuantity.Text);
        rqd.FVAT = float.Parse(this.txtVAT.Text);
        rqd.StrVoucher = this.txtVoucherNo.Text;

        //generate id
        ProRequest pr = new ProRequest();
        DataSet ds = pr.LoadRequestDetail(Request.Params.Get("rqid"));
        //DataSet ds = pr.LoadRequestDetail("RF-20100204-001");
        if (ds.Tables[0].Rows.Count > 0)
        {
            rqd.ICodeDetail = Int32.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["id"].ToString());
            rqd.ICodeDetail++;
        }
        else
            rqd.ICodeDetail = 1;

        rqd.FAmountVAT = rqd.FVAT * rqd.FAmountVND;
        rqd.FTotal = rqd.FAmountVAT + rqd.FAmountVND;

        return rqd;
    }

    private void ClearAllFields()
    {
        this.cldDateRequest.SelectedDate = DateTime.Now.Date;
        this.lblShowDate.Text = this.cldDateRequest.SelectedDate.ToShortDateString();
        this.txtVAT.Text = Constant.STR_DEFAULT;
        this.txtVoucherNo.Text = Constant.STR_DEFAULT;
        this.txtQuantity.Text = Constant.STR_DEFAULT;
        this.txtExRate.Text = Constant.STR_DEFAULT;
        this.txtDetail.Text = Constant.STR_DEFAULT;
        this.txtAmount.Text = Constant.STR_DEFAULT;
    }

    private void FillRequestDetail()
    {
        this.cldDateRequest.SelectedDate = DateTime.Parse(this.grdRequestDetail.SelectedRow.Cells[3].Text);
        this.lblShowDate.Text = this.cldDateRequest.SelectedDate.ToShortDateString();
        this.txtVoucherNo.Text = this.grdRequestDetail.SelectedRow.Cells[4].Text;
        this.txtDetail.Text = this.grdRequestDetail.SelectedRow.Cells[5].Text;
        this.txtQuantity.Text = this.grdRequestDetail.SelectedRow.Cells[6].Text;
        this.txtExRate.Text = this.grdRequestDetail.SelectedRow.Cells[8].Text;

        this.txtAmount.Text = this.grdRequestDetail.SelectedRow.Cells[7].Text;
        this.cbbVndUsd.SelectedIndex = 0;

        //USD didn't input -> set Currency is VND, and fill Amount is VNDAmount.
        if (this.grdRequestDetail.SelectedRow.Cells[7].Text == "")
        {
            this.cbbVndUsd.SelectedIndex = 1;
            this.txtAmount.Text = this.grdRequestDetail.SelectedRow.Cells[9].Text;
        }
        this.txtVAT.Text = this.grdRequestDetail.SelectedRow.Cells[10].Text;

        this.btnDelete.Enabled = true;
        this.btnNew.Enabled = false;
        this.btnSave.Enabled = true;

        this.isNew = false;
    }


}
